import Vue from 'vue'
import VueRouter from 'vue-router'
import { getLocal } from '@/utils/token.js'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/routerTabbar',
    name: 'routerTabbar',
    component: () => import('../views/routerTabar/routerTabbar.vue'),
    children: [
      {
        path: '/routerTabbar/home',
        name: 'home',
        component: () => import('../views/routerTabar/home/home.vue'),
        meta: {
          needTabbar: true
        }
      },
      {
        path: '/routerTabar/question',
        name: 'question',
        component: () => import('../views/routerTabar/question/question.vue'),
        meta: {
          needTabbar: true
        }
      },
      {
        path: '/routerTabbar/videox',
        name: 'videox',
        component: () => import('../views/routerTabar/videox/videox.vue'),
        meta: {
          needTabbar: true
        }
      },
      {
        path: '/routerTabbar/userInfo',
        name: 'userInfo',
        component: () => import('../views/routerTabar/userInfo/userInfo.vue'),
        meta: {
          needLogin: true, // 需要登录
          needTabbar: true // 需要tabbar栏
        }
      },
      {
        path: '/userInfo/infoEdit',
        name: 'infoEdit',
        component: () => import('../views/routerTabar/userInfo/infoEdit.vue'),
        meta: {
          needLogin: true // 需要登录
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 判断是否跳转到个人页面
  if (to.meta.needLogin) {
    // 判断是否有token
    if (getLocal()) {
      next()
    } else {
      next('/login') // 没有跳转到登录页面
    }
  } else {
    next()
  }
})
router.afterEach((to, from) => {})
export default router
